--
--	pickit for necromancer shrunken heads
--
--	picks up:
--		* magic/rare 5-6 spirit/spear 0/2 socket heads
--		* normal 3 spirit/spear 0/2 socket heads (good for low lvl duelling with rhyme or with splendor)
-- Thanks to ashashash! (and someone else?)

table.insert(itemActions,
	function(item, action)
		-- NORMAL/MAGIC necromancer shields
		-- see: dir .. "readme.txt" for information regarding these variables
		local priority = 4
		local identify = true
		local dump = true

		if not (isNormalNecroHead(item) or isMagicRareNecroHead(item)) then
			return false
		elseif action == aPICK and (not item.flags.Identified or
				isNormalNecroHead(item) or isGoodMagicRareNecroHead(item)) then
			return priority
		elseif action == aIDENT then
			return identify
		elseif action == aSELL and item.flags.Identified and not (
				isNormalNecroHead(item) or isGoodMagicRareNecroHead(item)) then
			return true
		elseif action == aDUMP and item.flags.Identified and not (
				isNormalNecroHead(item) or isGoodMagicRareNecroHead(item)) then
			return dump
		end

		return false
	end
)

function isMagicRareNecroHead(item)
	return (
			item.baseItem.baseType.Type == ItemKind.VoodooHeads and
			(item.quality == ItemQuality.Magic or item.quality == ItemQuality.Rare)
			)
end

function isNormalNecroHead(item)
	return (
			item.baseItem.baseType.Type == ItemKind.VoodooHeads and
			(item.quality == ItemQuality.Normal or item.quality == ItemQuality.Superior) and
			isGoodNormalNecroHead(item)
		)
end

function isGoodMagicRareNecroHead(item)
	local spear, spirit, pb, nec = 0, 0, 0, 0
	
	for i = 0, item.mods:size() - 1 do
		if (item.mods[i].Stat.Type == StatType.SingleSkill) then
			if (item.mods[i].Skill == SkillType.BoneSpirit) then
				spirit = item.mods[i].Value
			elseif (item.mods[i].Skill == SkillType.BoneSpear) then
				spear = item.mods[i].Value
			end
		elseif (item.mods[i].Stat.Type == StatType.ClassSkillsBonus) then
			nec = item.mods[i].Value
		elseif (item.mods[i].Stat.Type == StatType.SkillTabBonus and
				item.mods[i].Tab == SkillTab.PoisonAndBone) then
			pb = item.mods[i].Value
		end
	end
	
	return (
			(nec + pb + spear >= 3) or
			(nec + pb + spirit >= 3)
			)
end

function isGoodNormalNecroHead(item)
	for i = 0, item.stats:size() - 1 do
		if (item.stats[i].Stat.Type == StatType.Sockets) then
			if (item.stats[i].Value == 1) then
				return false
			end
		end
	end
	
	for i = 0, item.mods:size() - 1 do
		if (item.mods[i].Stat.Type == StatType.SingleSkill) then
			if (item.mods[i].Skill == SkillType.BoneSpirit or
				item.mods[i].Skill == SkillType.BoneSpear) then
				if (item.mods[i].Value==3) then
					return true
				end
			end
		end
	end
	
	return false
end
